//package com.origin.src.datasource;
//
//import com.zaxxer.hikari.HikariConfig;
//import com.zaxxer.hikari.HikariDataSource;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
//import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
//import org.apache.shardingsphere.api.config.sharding.strategy.InlineShardingStrategyConfiguration;
//import org.apache.shardingsphere.api.config.sharding.strategy.StandardShardingStrategyConfiguration;
//import org.apache.shardingsphere.encrypt.api.EncryptColumnRuleConfiguration;
//import org.apache.shardingsphere.encrypt.api.EncryptRuleConfiguration;
//import org.apache.shardingsphere.encrypt.api.EncryptTableRuleConfiguration;
//import org.apache.shardingsphere.encrypt.api.EncryptorRuleConfiguration;
//import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
//import org.springframework.context.annotation.Bean;
//import org.springframework.stereotype.Component;
//
//import javax.sql.DataSource;
//import java.sql.Connection;
//import java.sql.SQLException;
//import java.sql.Statement;
//import java.util.Collections;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.Properties;
//
//@Component
//@Slf4j
//public class ShardingDataSource {
//    static String dbPrefix = "app_";
//    static int dbMax = 1;
//    static int tableMax = 3;
//
//    @Bean("dataSource")
////    @ConditionalOnMissingBean(DataSource.class)
//    public static DataSource dataSource() throws SQLException {
//        Map<String, DataSource> dataSourceMap = new HashMap<>();
//        for (int i = 0; i <= dbMax; i++) {
//            String dbName = dbPrefix + i;
//            DataSource dataSource = createDataSource(dbName);
//            dataSourceMap.put(dbName, dataSource);
//        }
//
//        TableRuleConfiguration userTbConf = getTableRuleConfiguration("t_user", dbPrefix, "user_id", "user_id", dbMax, tableMax);
//
//        TableRuleConfiguration orderTbConf = getTableRuleConfiguration("t_order", dbPrefix, "user_id", "user_id", dbMax, tableMax);
//
//        // 配置分片规则
//        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
//        shardingRuleConfig.getTableRuleConfigs().add(userTbConf);
//        shardingRuleConfig.getTableRuleConfigs().add(orderTbConf);
//        //脱敏规则
//        shardingRuleConfig.setEncryptRuleConfig(getEncryptRuleConfiguration());
//
//        return ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new Properties());
//    }
//
//    private static TableRuleConfiguration getTableRuleConfiguration(String logicTable, String dbPrefix, String dbShardingColumn, String tableShardingColumn, int dbMax, int tableMax) {
//        TableRuleConfiguration ruleConf = new TableRuleConfiguration(
//                logicTable, dbPrefix + "${0.." + dbMax + "}." + logicTable + "_${0.." + tableMax + "}");
//
//        int dbSize = dbMax + 1;
//        ruleConf.setDatabaseShardingStrategyConfig(
//                new InlineShardingStrategyConfiguration(dbShardingColumn, dbPrefix + "${" + dbShardingColumn + " % " + dbSize + "}"));
//
//        ruleConf.setTableShardingStrategyConfig(
//                new StandardShardingStrategyConfiguration(tableShardingColumn, new PreciseShardingAlgorithmCRC32(logicTable + "_")));
//        return ruleConf;
//    }
//
//    public static DataSource createDataSource(String dbName) {
//        HikariConfig hikariConfig = new HikariConfig();
//        hikariConfig.setJdbcUrl("jdbc:mysql://localhost:3306/" + dbName + "?serverTimezone=GMT%2B8&autoReconnect=true&failOverReadOnly=false&autoReconnect=true&autoReconnectForPools=true");
//        hikariConfig.setUsername("root");
//        hikariConfig.setPassword("root");
//        hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
//        hikariConfig.setMaximumPoolSize(8);
//        hikariConfig.setMinimumIdle(0);
//        hikariConfig.setPoolName(dbName);
//        return new HikariDataSource(hikariConfig);
//    }
//
//    private static EncryptRuleConfiguration getEncryptRuleConfiguration() {
//
//        EncryptColumnRuleConfiguration columnConfig = new EncryptColumnRuleConfiguration("", "login_password", "", "aes");
//        EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration(Collections.singletonMap("login_password", columnConfig));
//
//        Properties props = new Properties();
//        props.setProperty("aes.key.value", "AAAAAAAAAAAAAAAA");
//        EncryptorRuleConfiguration encryptorConfig = new EncryptorRuleConfiguration("AES", props);
//
//        EncryptRuleConfiguration encryptRuleConfig = new EncryptRuleConfiguration();
//        encryptRuleConfig.getEncryptors().put("aes", encryptorConfig);
//        encryptRuleConfig.getTables().put("t_user", tableConfig);
//
//        return encryptRuleConfig;
//    }
//
//    public static void main(String[] args) {
//        try {
//            for (int i = 0; i <= dbMax; i++) {
//                String dbName = dbPrefix + i;
//                DataSource dataSource = createDataSource(dbName);
//                Connection connection = dataSource.getConnection();
//                Statement statement = connection.createStatement();
//
//                for (int t = 0; t <= tableMax; t++) {
//                    statement.execute("DROP TABLE IF EXISTS `t_user_" + t + "`");
//                    statement.execute("CREATE TABLE `t_user_" + t + "` (\n" +
//                            "  `user_id` bigint NOT NULL,\n" +
//                            "  `user_name` varchar(255) DEFAULT NULL,\n" +
//                            "  `user_phone` varchar(255) DEFAULT NULL,\n" +
//                            "  `user_email` varchar(255) DEFAULT NULL,\n" +
//                            "  `login_password` varchar(255) DEFAULT NULL,\n" +
//                            "  PRIMARY KEY (`user_id`)\n" +
//                            ")");
//
//                    statement.execute("DROP TABLE IF EXISTS `t_order_" + t + "`");
//                    statement.execute("CREATE TABLE `t_order_" + t + "` (\n" +
//                            "  `order_id` bigint NOT NULL,\n" +
//                            "  `user_id` bigint NOT NULL,\n" +
//                            "  `order_name` varchar(255) DEFAULT NULL,\n" +
//                            "  `pay_amount` NUMERIC(15,2) DEFAULT NULL,\n" +
//                            "  `user_address` varchar(255) DEFAULT NULL,\n" +
//                            "  PRIMARY KEY (`order_id`)\n" +
//                            ")");
//                }
//            }
//        } catch (Exception e) {
//            log.error("db error", e);
//        }
//
//
//    }
//}
